package genetic.q4;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;

public class EternityPiece {

	private static final int NORTH = 0;
	private static final int EAST = 1;
	private static final int SOUTH = 2;
	private static final int WEST = 3;

	private final int east;
	private final int west;
	private final int north;
	private final int south;
	public EternityPiece(int north, int east, int south, int west) {
		this.north = north;
		this.east = east;
		this.west = west;
		this.south = south;
	}

	public int getNorth() {
		return north;
	}

	public int getWest() {
		return west;
	}

	public int getEast() {
		return east;
	}

	public int getSouth() {
		return south;
	}
	public int getColor(int firstColor, int secondPlaceOffset){
		if (north==firstColor){
			return colorInOffset(NORTH,secondPlaceOffset);
		}
		if (east==firstColor){
			return colorInOffset(EAST,secondPlaceOffset);
		}
		if (south==firstColor){
			return colorInOffset(SOUTH,secondPlaceOffset);
		}
		if (west==firstColor){
			return colorInOffset(WEST,secondPlaceOffset);
		}
		return -1;
	}

	public int getColor(int firstColor, int secondPlaceOffset, int secondColor, int thirdPlaceOffset){
		if (north==firstColor && colorInOffset(NORTH, secondPlaceOffset) == secondColor){
			return colorInOffset(NORTH,thirdPlaceOffset);
		}
		if (east==firstColor && colorInOffset(EAST, secondPlaceOffset) == secondColor){
			return colorInOffset(EAST,thirdPlaceOffset);
		}
		if (south==firstColor && colorInOffset(SOUTH, secondPlaceOffset) == secondColor){
			return colorInOffset(SOUTH,thirdPlaceOffset);
		}
		if (west==firstColor && colorInOffset(WEST, secondPlaceOffset) == secondColor){
			return colorInOffset(WEST,thirdPlaceOffset);
		}
		return -1;
	}

	public int getColor(int firstColor, int secondPlaceOffset, int secondColor, int thirdPlaceOffset, int thirdPlaceColor){
		if (north==firstColor && colorInOffset(NORTH, secondPlaceOffset) == secondColor && colorInOffset(NORTH, thirdPlaceOffset) == thirdPlaceColor){
			return colorInOffset(NORTH,1+2+3-secondPlaceOffset-thirdPlaceOffset);
		}
		if (east==firstColor && colorInOffset(EAST, secondPlaceOffset) == secondColor && colorInOffset(EAST, thirdPlaceOffset) == thirdPlaceColor){
			return colorInOffset(EAST,1+2+3-secondPlaceOffset-thirdPlaceOffset);
		}
		if (south==firstColor && colorInOffset(SOUTH, secondPlaceOffset) == secondColor && colorInOffset(SOUTH, thirdPlaceOffset) == thirdPlaceColor){
			return colorInOffset(SOUTH,1+2+3-secondPlaceOffset-thirdPlaceOffset);
		}
		if (west==firstColor && colorInOffset(WEST, secondPlaceOffset) == secondColor && colorInOffset(WEST, thirdPlaceOffset) == thirdPlaceColor){
			return colorInOffset(WEST,1+2+3-secondPlaceOffset-thirdPlaceOffset);
		}
		return -1;
	}

	public boolean fits(int baseColor, int offset, int requestedColor){
		boolean tmp = false;
		if (north==baseColor){
			tmp = tmp || colorInOffset(NORTH, offset) == requestedColor;
		}
		if (east==baseColor){
			tmp = tmp || colorInOffset(EAST, offset) == requestedColor;
		}
		if (south==baseColor){
			tmp = tmp || colorInOffset(SOUTH, offset) == requestedColor;
		}
		if (west==baseColor){
			tmp = tmp || colorInOffset(WEST, offset) == requestedColor;
		}
		return tmp;
	}
	
	public boolean fits(int baseColor, int offset, int requestedColor, int offset2, int reqestedColor2){
		boolean tmp = false;
		if (north==baseColor && colorInOffset(NORTH, offset) == requestedColor){
			tmp = tmp || colorInOffset(NORTH,offset2) == reqestedColor2;
		}
		if (east==baseColor && colorInOffset(EAST, offset) == requestedColor){
			tmp = tmp || colorInOffset(EAST,offset2) == reqestedColor2;
		}
		if (south==baseColor && colorInOffset(SOUTH, offset) == requestedColor){
			tmp = tmp || colorInOffset(SOUTH,offset2) == reqestedColor2;
		}
		if (west==baseColor && colorInOffset(WEST, offset) == requestedColor){
			tmp = tmp || colorInOffset(WEST,offset2) == reqestedColor2;
		}
		return tmp;
	}

	private int colorInOffset(int org, int offset){
		switch((org+offset) % 4){
		case 0:	return north;
		case 1: return east;
		case 2: return south;
		case 3: return west;
		}
		return -1;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof EternityPiece)) return false;
		EternityPiece other = (EternityPiece)obj;
		return this.north == other.north && this.east == other.east && this.south == other.south && this.west == other.west;
	}
	
	@Override
	public String toString() {
		return "{"+north+","+east+","+south+","+west+"}";
	}


	public static final int GRAY = 0;
	public static final int ONE = 1;
	public static final int TWO = 2;
	public static final int THREE = 3;
	public static final int FOUR = 4;
	public static final int FIVE = 5;
	public static final int SIX = 6;
	public static final int SEVEN = 7;
	public static final int EIGHT = 8;
	public static final int NINE = 9;
	public static final int TEN = 10;
	public static final int ELEVEN = 11;
	public static final int TWELVE = 12;
	public static final int THIRTEEN = 13;
	public static final int FOURTEEN = 14;
	public static final int FIFTEEN = 15;
	public static final int SIXTEEN = 16;
	public static final int SEVENTEEN = 17;
	public static final int EIGHTEEN = 18;
	public static final int NINETEEN = 19;
	public static final int TWENTY = 20;
	public static final int TWENTYONE = 21;
	public static final int TWENTYTWO = 22;
	public static final int FAKE = 23;
	public static TreeMap<Integer, EternityPiece> dict = new TreeMap<Integer, EternityPiece>();
	public static TreeMap<Integer, List<EternityPiece> > colormap = new TreeMap<Integer, List<EternityPiece> >();

	static{

		//colormap = new TreeMap<Integer, List<EternityPiece> >();
		//create all of the pieces and add them to dictionary
		//A
		//first row	- OK

		dict.put(1	,new EternityPiece(TWO		,ONE	,GRAY 	,GRAY));
		dict.put(2	,new EternityPiece(TWO		,THREE	,GRAY 	,GRAY));
		dict.put(3	,new EternityPiece(FOUR		,ONE	,GRAY 	,GRAY));
		dict.put(4	,new EternityPiece(ONE		,FOUR	,GRAY	,GRAY));
		dict.put(5	,new EternityPiece(SIX		,TWO	,GRAY	,TWO));
		dict.put(6	,new EternityPiece(SEVEN	,FOUR	,GRAY	,TWO));
		dict.put(7	,new EternityPiece(EIGHT	,TWO	,GRAY	,TWO));
		dict.put(8	,new EternityPiece(EIGHT	,FIVE	,GRAY	,TWO));
		//second row - OK
		dict.put(9	,new EternityPiece(NINE			,ONE	,GRAY	,TWO));
		dict.put(10	,new EternityPiece(TWENTYTWO	,THREE	,GRAY	,TWO));
		dict.put(11	,new EternityPiece(TWELVE		,FOUR	,GRAY	,TWO));
		dict.put(12	,new EternityPiece(EIGHTEEN		,THREE	,GRAY	,TWO));
		dict.put(13	,new EternityPiece(EIGHTEEN		,FIVE	,GRAY	,TWO));
		dict.put(14	,new EternityPiece(SIXTEEN		,THREE	,GRAY	,TWO));
		dict.put(15	,new EternityPiece(SEVEN		,TWO	,GRAY	,FOUR));
		dict.put(16	,new EternityPiece(SEVENTEEN	,ONE	,GRAY	,FOUR));
		//third row - OK
		dict.put(17	,new EternityPiece(TEN			,FIVE	,GRAY	,FOUR));
		dict.put(18	,new EternityPiece(ELEVEN		,FIVE	,GRAY	,FOUR));
		dict.put(19	,new EternityPiece(TWENTYTWO	,FOUR	,GRAY	,FOUR));
		dict.put(20	,new EternityPiece(TWELVE		,FOUR	,GRAY	,FOUR));
		dict.put(21	,new EternityPiece(THIRTEEN		,THREE	,GRAY	,FOUR));
		dict.put(22	,new EternityPiece(FOURTEEN		,TWO	,GRAY	,FOUR));
		dict.put(23	,new EternityPiece(FOURTEEN		,FIVE	,GRAY	,FOUR));
		dict.put(24	,new EternityPiece(FIFTEEN		,TWO	,GRAY	,FOUR));
		//fourth row - OK
		dict.put(25	,new EternityPiece(SIXTEEN		,TWO	,GRAY	,FOUR));
		dict.put(26	,new EternityPiece(SIX			,FOUR	,GRAY	,ONE));
		dict.put(27	,new EternityPiece(SIX			,ONE	,GRAY	,ONE));
		dict.put(28	,new EternityPiece(SEVEN		,ONE	,GRAY	,ONE));
		dict.put(29	,new EternityPiece(SEVENTEEN	,ONE	,GRAY	,ONE));
		dict.put(30	,new EternityPiece(TWENTYTWO	,FIVE	,GRAY	,ONE));
		dict.put(31	,new EternityPiece(TWELVE		,FOUR	,GRAY	,ONE));
		dict.put(32	,new EternityPiece(FIFTEEN		,ONE	,GRAY	,ONE));
		//fifth row - OK
		dict.put(33	,new EternityPiece(EIGHTEEN		,FOUR	,GRAY	,ONE));
		dict.put(34	,new EternityPiece(EIGHTEEN		,THREE	,GRAY	,ONE));
		dict.put(35	,new EternityPiece(NINETEEN		,FIVE	,GRAY	,ONE));
		dict.put(36	,new EternityPiece(TWENTY		,THREE	,GRAY	,ONE));
		dict.put(37	,new EternityPiece(SEVENTEEN	,TWO	,GRAY	,THREE));
		dict.put(38	,new EternityPiece(TWENTYONE	,FIVE	,GRAY	,THREE));
		dict.put(39	,new EternityPiece(NINE			,FIVE	,GRAY	,THREE));
		dict.put(40	,new EternityPiece(ELEVEN		,FOUR	,GRAY	,THREE));
		//sixth row - OK
		dict.put(41	,new EternityPiece(ELEVEN		,ONE	,GRAY	,THREE));
		dict.put(42	,new EternityPiece(TWELVE		,TWO	,GRAY	,THREE));
		dict.put(43	,new EternityPiece(TWELVE		,FOUR	,GRAY	,THREE));
		dict.put(44	,new EternityPiece(TWELVE		,ONE	,GRAY	,THREE));
		dict.put(45	,new EternityPiece(THIRTEEN		,TWO	,GRAY	,THREE));
		dict.put(46	,new EternityPiece(FIFTEEN		,THREE	,GRAY	,THREE));
		dict.put(47	,new EternityPiece(EIGHTEEN		,THREE	,GRAY	,THREE));
		dict.put(48	,new EternityPiece(NINETEEN		,THREE	,GRAY	,THREE));
		//B
		//first row - OK
		dict.put(49	,new EternityPiece(SIX		,FIVE	,GRAY	,FIVE));
		dict.put(50	,new EternityPiece(SEVEN	,TWO	,GRAY	,FIVE));
		dict.put(51	,new EternityPiece(SEVEN	,FOUR	,GRAY	,FIVE));
		dict.put(52	,new EternityPiece(EIGHT	,TWO	,GRAY	,FIVE));
		dict.put(53	,new EternityPiece(TWENTYTWO,THREE	,GRAY	,FIVE));
		dict.put(54	,new EternityPiece(THIRTEEN	,THREE	,GRAY	,FIVE));
		dict.put(55	,new EternityPiece(THIRTEEN	,FIVE	,GRAY	,FIVE));
		dict.put(56	,new EternityPiece(EIGHTEEN	,ONE	,GRAY	,FIVE));
		//second row - OK
		dict.put(57	,new EternityPiece(NINETEEN	,TWO		,GRAY	,FIVE));
		dict.put(58	,new EternityPiece(NINETEEN	,FIVE		,GRAY	,FIVE));
		dict.put(59	,new EternityPiece(SIXTEEN	,FOUR		,GRAY	,FIVE));
		dict.put(60	,new EternityPiece(TWENTY	,ONE		,GRAY	,FIVE));
		dict.put(61	,new EternityPiece(EIGHT	,SEVENTEEN	,SIX	,SIX));
		dict.put(62	,new EternityPiece(TEN		,TWENTYTWO	,SIX	,SIX));
		dict.put(63	,new EternityPiece(SEVEN	,TWENTYONE	,SIX	,SEVEN));
		dict.put(64	,new EternityPiece(SIX		,EIGHTEEN	,SIX	,SEVENTEEN));
		//third row - OK
		dict.put(65	,new EternityPiece(SEVENTEEN	,TWENTY		,SIX	,SEVENTEEN));
		dict.put(66	,new EternityPiece(TEN			,TEN		,SIX	,SEVENTEEN));
		dict.put(67	,new EternityPiece(NINE			,SEVEN		,SIX	,SEVENTEEN));
		dict.put(68	,new EternityPiece(FIFTEEN		,EIGHT		,SIX	,SEVENTEEN));
		dict.put(69	,new EternityPiece(TWENTY		,EIGHTEEN	,SIX	,SEVENTEEN));
		dict.put(70	,new EternityPiece(TWENTYONE	,TWENTYTWO	,SIX	,TWENTYONE));
		dict.put(71	,new EternityPiece(TWENTYTWO	,FOURTEEN	,SIX	,TWENTYONE));
		dict.put(72	,new EternityPiece(TEN			,SEVENTEEN	,SIX	,NINE));
		//fourth row - OK
		dict.put(73	,new EternityPiece(TWELVE		,THIRTEEN	,SIX	,NINE));
		dict.put(74	,new EternityPiece(FIFTEEN		,TWELVE		,SIX	,NINE));
		dict.put(75	,new EternityPiece(EIGHTEEN		,TWENTYONE	,SIX	,NINE));
		dict.put(76	,new EternityPiece(TEN			,TWELVE		,SIX	,ELEVEN));
		dict.put(77	,new EternityPiece(ELEVEN		,TWELVE		,SIX	,ELEVEN));
		dict.put(78	,new EternityPiece(TWENTYONE	,NINETEEN	,SIX	,TWENTYTWO));
		dict.put(79	,new EternityPiece(FIFTEEN		,TWENTYONE	,SIX	,TWENTYTWO));
		dict.put(80	,new EternityPiece(NINETEEN		,SIXTEEN	,SIX	,TWENTYTWO));
		//fifth row - OK
		dict.put(81	,new EternityPiece(ELEVEN		,SEVENTEEN	,SIX	,TWELVE));
		dict.put(82	,new EternityPiece(SEVENTEEN	,SEVENTEEN	,SIX	,THIRTEEN));
		dict.put(83	,new EternityPiece(NINE			,THIRTEEN	,SIX	,THIRTEEN));
		dict.put(84	,new EternityPiece(SEVENTEEN	,ELEVEN		,SIX	,FOURTEEN));
		dict.put(85	,new EternityPiece(EIGHT		,TEN		,SIX	,FOURTEEN));
		dict.put(86	,new EternityPiece(EIGHTEEN		,FOURTEEN	,SIX	,FOURTEEN));
		dict.put(87	,new EternityPiece(NINETEEN		,FIFTEEN	,SIX	,FOURTEEN));
		dict.put(88	,new EternityPiece(SIX			,SIXTEEN	,SIX	,FIFTEEN));
		//sixth row - OK
		dict.put(89	,new EternityPiece(EIGHT	,TWENTY		,SIX	,FIFTEEN));
		dict.put(90	,new EternityPiece(THIRTEEN	,NINETEEN	,SIX	,FIFTEEN));
		dict.put(91	,new EternityPiece(NINE		,FOURTEEN	,SIX	,EIGHTEEN));
		dict.put(92	,new EternityPiece(ELEVEN	,TWELVE		,SIX	,EIGHTEEN));
		dict.put(93	,new EternityPiece(ELEVEN	,THIRTEEN	,SIX	,EIGHTEEN));
		dict.put(94	,new EternityPiece(THIRTEEN	,SIXTEEN	,SIX	,EIGHTEEN));
		dict.put(95	,new EternityPiece(FOURTEEN	,TEN		,SIX	,EIGHTEEN));
		dict.put(96	,new EternityPiece(SIXTEEN	,TWENTYONE	,SIX	,SIXTEEN));
		//C
		//first row
		dict.put(97		,new EternityPiece(THIRTEEN	,ELEVEN		,SIX	,TWENTY));
		dict.put(98		,new EternityPiece(FIFTEEN	,EIGHTEEN	,SIX	,TWENTY));
		dict.put(99		,new EternityPiece(SIXTEEN	,EIGHT		,SIX	,TWENTY));
		dict.put(100	,new EternityPiece(TWENTY	,SIXTEEN	,SIX	,TWENTY));
		dict.put(101	,new EternityPiece(FOURTEEN	,TWELVE	,SEVEN	,SEVEN));
		dict.put(102	,new EternityPiece(FOURTEEN	,NINETEEN	,SEVEN	,SEVEN));
		dict.put(103	,new EternityPiece(NINETEEN	,ELEVEN	,SEVEN	,SEVEN));
		dict.put(104	,new EternityPiece(TWENTY	,EIGHT	,SEVEN	,SEVEN));
		//second row
		dict.put(105	,new EternityPiece(THIRTEEN		,TWELVE		,SEVEN	,SEVENTEEN));
		dict.put(106	,new EternityPiece(TWENTYONE	,EIGHTEEN	,SEVEN	,EIGHT));
		dict.put(107	,new EternityPiece(ELEVEN		,EIGHTEEN	,SEVEN	,EIGHT));
		dict.put(108	,new EternityPiece(THIRTEEN		,TWELVE		,SEVEN	,EIGHT));
		dict.put(109	,new EternityPiece(NINETEEN		,NINE		,SEVEN	,EIGHT));
		dict.put(110	,new EternityPiece(TWELVE		,FOURTEEN	,SEVEN	,TEN));
		dict.put(111	,new EternityPiece(FOURTEEN		,TWELVE		,SEVEN	,TEN));
		dict.put(112	,new EternityPiece(FIFTEEN		,ELEVEN		,SEVEN	,TWENTYONE));
		//third row
		dict.put(113	,new EternityPiece(FIFTEEN		,NINETEEN	,SEVEN	,TWENTYONE));
		dict.put(114	,new EternityPiece(TEN			,THIRTEEN	,SEVEN	,NINE));
		dict.put(115	,new EternityPiece(TWENTYTWO	,FOURTEEN	,SEVEN	,NINE));
		dict.put(116	,new EternityPiece(FOURTEEN		,NINE		,SEVEN	,NINE));
		dict.put(117	,new EternityPiece(TWENTY		,NINETEEN	,SEVEN	,NINE));
		dict.put(118	,new EternityPiece(TWENTYONE	,SIXTEEN	,SEVEN	,ELEVEN));
		dict.put(119	,new EternityPiece(NINETEEN		,FOURTEEN	,SEVEN	,TWENTYTWO));
		dict.put(120	,new EternityPiece(EIGHTEEN		,TWENTY		,SEVEN	,TWELVE));
		//fourth row
		dict.put(121	,new EternityPiece(TEN		,TWENTY		,SEVEN	,THIRTEEN));
		dict.put(122	,new EternityPiece(EIGHT	,NINETEEN	,SEVEN	,FIFTEEN));
		dict.put(123	,new EternityPiece(TEN		,ELEVEN		,SEVEN	,FIFTEEN));
		dict.put(124	,new EternityPiece(FIFTEEN	,TWELVE		,SEVEN	,FIFTEEN));
		dict.put(125	,new EternityPiece(FOURTEEN	,TWENTY		,SEVEN	,EIGHTEEN));
		dict.put(126	,new EternityPiece(SIXTEEN	,TWELVE		,SEVEN	,EIGHTEEN));
		dict.put(127	,new EternityPiece(TEN		,EIGHT		,SEVEN	,NINETEEN));
		dict.put(128	,new EternityPiece(ELEVEN	,SIXTEEN	,SEVEN	,NINETEEN));
		//fifth row
		dict.put(129	,new EternityPiece(THIRTEEN	,TWENTYONE	,SEVEN	,NINETEEN));
		dict.put(130	,new EternityPiece(FIFTEEN	,EIGHTEEN	,SEVEN	,NINETEEN));
		dict.put(131	,new EternityPiece(EIGHT	,FIFTEEN	,SEVEN	,SIXTEEN));
		dict.put(132	,new EternityPiece(FOURTEEN	,TEN		,SEVEN	,SIXTEEN));
		dict.put(133	,new EternityPiece(TEN		,NINETEEN	,SEVEN	,TWENTY));
		dict.put(134	,new EternityPiece(NINE		,THIRTEEN	,SEVEN	,TWENTY));
		dict.put(135	,new EternityPiece(THIRTEEN	,TWENTYONE	,SEVEN	,TWENTY));
		dict.put(136	,new EternityPiece(NINETEEN	,FIFTEEN	,SEVEN	,TWENTY));
		//sixth row
		dict.put(137	,new EternityPiece(FIFTEEN	,TWENTYTWO	,SEVENTEEN	,SEVENTEEN));
		dict.put(138	,new EternityPiece(EIGHT	,TWENTYONE	,SEVENTEEN	,EIGHT));
		//dict.put(139	,new EternityPiece(EIGHT	,NINE		,SEVENTEEN	,EIGHT));
		dict.put(139	,new EternityPiece(FAKE		,FAKE		,FAKE		,FAKE));	//this piece has a special place
		dict.put(140	,new EternityPiece(EIGHT	,FOURTEEN	,SEVENTEEN	,EIGHT));
		dict.put(141	,new EternityPiece(ELEVEN	,SIXTEEN	,SEVENTEEN	,EIGHT));
		dict.put(142	,new EternityPiece(TWELVE	,EIGHT		,SEVENTEEN	,EIGHT));
		dict.put(143	,new EternityPiece(NINETEEN	,FOURTEEN	,SEVENTEEN	,EIGHT));
		dict.put(144	,new EternityPiece(SIXTEEN	,SIXTEEN	,SEVENTEEN	,EIGHT));
		//D
		//first row
		dict.put(145	,new EternityPiece(TWENTYONE	,THIRTEEN	,SEVENTEEN	,TEN));
		dict.put(146	,new EternityPiece(SEVENTEEN	,FOURTEEN	,SEVENTEEN	,TWENTYONE));
		dict.put(147	,new EternityPiece(SEVENTEEN	,TWENTY		,SEVENTEEN	,TWENTYONE));
		dict.put(148	,new EternityPiece(TWENTYONE	,TEN		,SEVENTEEN	,TWENTYONE));
		dict.put(149	,new EternityPiece(TWELVE		,FOURTEEN	,SEVENTEEN	,TWENTYONE));
		dict.put(150	,new EternityPiece(EIGHT		,NINE		,SEVENTEEN	,ELEVEN));
		dict.put(151	,new EternityPiece(THIRTEEN		,TWENTY		,SEVENTEEN	,ELEVEN));
		dict.put(152	,new EternityPiece(NINE			,NINE		,SEVENTEEN	,TWENTYTWO));
		//second row
		dict.put(153	,new EternityPiece(NINE			,ELEVEN		,SEVENTEEN	,TWENTYTWO));
		dict.put(154	,new EternityPiece(TWENTY		,NINETEEN	,SEVENTEEN	,TWENTYTWO));
		dict.put(155	,new EternityPiece(TWENTYTWO	,TWENTYTWO	,SEVENTEEN	,THIRTEEN));
		dict.put(156	,new EternityPiece(TWENTYTWO	,FOURTEEN	,SEVENTEEN	,THIRTEEN));
		dict.put(157	,new EternityPiece(TWENTY		,TWENTYTWO	,SEVENTEEN	,THIRTEEN));
		dict.put(158	,new EternityPiece(TWENTYTWO	,NINETEEN	,SEVENTEEN	,FIFTEEN));
		dict.put(159	,new EternityPiece(EIGHTEEN		,EIGHT		,SEVENTEEN	,FIFTEEN));
		dict.put(160	,new EternityPiece(SIXTEEN		,SIXTEEN	,SEVENTEEN	,EIGHTEEN));
		//third row	
		dict.put(161	,new EternityPiece(EIGHT		,FIFTEEN	,SEVENTEEN	,NINETEEN));
		dict.put(162	,new EternityPiece(TEN			,FIFTEEN	,SEVENTEEN	,NINETEEN));
		dict.put(163	,new EternityPiece(TWELVE		,NINE		,SEVENTEEN	,TWENTY));
		dict.put(164	,new EternityPiece(THIRTEEN		,NINETEEN	,SEVENTEEN	,TWENTY));
		dict.put(165	,new EternityPiece(TWENTYONE	,THIRTEEN	,EIGHT		,TEN));
		dict.put(166	,new EternityPiece(THIRTEEN		,EIGHTEEN	,EIGHT		,TEN));
		dict.put(167	,new EternityPiece(TWENTYONE	,TWENTYONE	,EIGHT		,NINE));
		dict.put(168	,new EternityPiece(NINE			,TWELVE		,EIGHT		,ELEVEN));
		//fourth row
		dict.put(169	,new EternityPiece(ELEVEN		,SIXTEEN	,EIGHT	,ELEVEN));
		dict.put(170	,new EternityPiece(THIRTEEN		,EIGHTEEN	,EIGHT	,TWENTYTWO));
		dict.put(171	,new EternityPiece(FIFTEEN		,NINETEEN	,EIGHT	,TWENTYTWO));
		dict.put(172	,new EternityPiece(SIXTEEN		,NINE		,EIGHT	,TWENTYTWO));
		dict.put(173	,new EternityPiece(TWELVE		,TWELVE		,EIGHT	,TWELVE));
		dict.put(174	,new EternityPiece(FOURTEEN		,FIFTEEN	,EIGHT	,TWELVE));
		dict.put(175	,new EternityPiece(TWENTYTWO	,SIXTEEN	,EIGHT	,THIRTEEN));
		dict.put(176	,new EternityPiece(TWENTYTWO	,ELEVEN		,EIGHT	,FOURTEEN));
		//fifth row
		dict.put(177	,new EternityPiece(TEN			,THIRTEEN	,EIGHT	,FIFTEEN));
		dict.put(178	,new EternityPiece(FOURTEEN		,NINETEEN	,EIGHT	,EIGHTEEN));
		dict.put(179	,new EternityPiece(EIGHTEEN		,TWELVE		,EIGHT	,EIGHTEEN));
		dict.put(180	,new EternityPiece(TWENTYTWO	,NINETEEN	,EIGHT	,NINETEEN));
		dict.put(181	,new EternityPiece(NINE			,NINETEEN	,EIGHT	,SIXTEEN));
		dict.put(182	,new EternityPiece(TWENTYTWO	,NINE		,EIGHT	,SIXTEEN));
		dict.put(183	,new EternityPiece(ELEVEN		,EIGHTEEN	,TEN	,TEN));
		dict.put(184	,new EternityPiece(TWENTY		,TWENTYTWO	,TEN	,TWENTYONE));
		//sixth row
		dict.put(185	,new EternityPiece(ELEVEN		,FOURTEEN	,TEN	,NINE));
		dict.put(186	,new EternityPiece(EIGHTEEN		,EIGHTEEN	,TEN	,NINE));
		dict.put(187	,new EternityPiece(SIXTEEN		,TWENTYTWO	,TEN	,ELEVEN));
		dict.put(188	,new EternityPiece(TEN			,SIXTEEN	,TEN	,TWENTYTWO));
		dict.put(189	,new EternityPiece(TWENTYONE	,ELEVEN		,TEN	,TWENTYTWO));
		dict.put(190	,new EternityPiece(NINETEEN		,ELEVEN		,TEN	,TWENTYTWO));
		dict.put(191	,new EternityPiece(TWENTYONE	,TWENTYONE	,TEN	,TWELVE));
		dict.put(192	,new EternityPiece(TWELVE		,FIFTEEN	,TEN	,TWELVE));
		//E
		//first row
		dict.put(193	,new EternityPiece(NINE		,TWENTYTWO	,TEN	,THIRTEEN));
		dict.put(194	,new EternityPiece(SIXTEEN	,NINE		,TEN	,FOURTEEN));
		dict.put(195	,new EternityPiece(SIXTEEN	,TWENTY		,TEN	,FOURTEEN));
		dict.put(196	,new EternityPiece(TWENTY	,TWELVE		,TEN	,FOURTEEN));
		dict.put(197	,new EternityPiece(NINE		,TWENTY	,TEN	,FIFTEEN));
		dict.put(198	,new EternityPiece(ELEVEN	,EIGHTEEN	,TEN	,FIFTEEN));
		dict.put(199	,new EternityPiece(FIFTEEN	,FIFTEEN	,TEN	,FIFTEEN));
		dict.put(200	,new EternityPiece(ELEVEN	,TWENTYONE	,TEN	,EIGHTEEN));
		//second row
		dict.put(201	,new EternityPiece(SIXTEEN	,EIGHTEEN	,TEN		,NINETEEN));
		dict.put(202	,new EternityPiece(TWENTY	,TWENTYTWO	,TEN		,NINETEEN));
		dict.put(203	,new EternityPiece(FOURTEEN	,ELEVEN		,TEN		,SIXTEEN));
		dict.put(204	,new EternityPiece(FOURTEEN	,EIGHTEEN	,TEN		,SIXTEEN));
		dict.put(205	,new EternityPiece(NINETEEN	,TWENTYONE	,TEN		,SIXTEEN));
		dict.put(206	,new EternityPiece(TWENTY	,SIXTEEN	,TEN		,SIXTEEN));
		dict.put(207	,new EternityPiece(TWENTY	,TWENTYTWO	,TWENTYONE	,TWENTYONE));
		dict.put(208	,new EternityPiece(ELEVEN	,TWENTY	,TWENTYONE	,NINE));
		//third row
		dict.put(209	,new EternityPiece(EIGHTEEN		,TWELVE		,TWENTYONE	,NINE));
		dict.put(210	,new EternityPiece(NINE			,ELEVEN		,TWENTYONE	,TWENTYTWO));
		dict.put(211	,new EternityPiece(NINETEEN		,TWELVE		,TWENTYONE	,TWENTYTWO));
		dict.put(212	,new EternityPiece(TWENTYONE	,NINETEEN	,TWENTYONE	,TWELVE));
		dict.put(213	,new EternityPiece(EIGHTEEN		,EIGHTEEN	,TWENTYONE	,THIRTEEN));
		dict.put(214	,new EternityPiece(TWENTYONE	,FIFTEEN	,TWENTYONE	,FOURTEEN));
		dict.put(215	,new EternityPiece(THIRTEEN		,TWENTY		,TWENTYONE	,FOURTEEN));
		dict.put(216	,new EternityPiece(TWENTY		,SIXTEEN	,TWENTYONE	,FOURTEEN));
		//fourth row
		dict.put(217	,new EternityPiece(ELEVEN		,TWELVE		,TWENTYONE	,FIFTEEN));
		dict.put(218	,new EternityPiece(THIRTEEN		,FOURTEEN	,TWENTYONE	,NINETEEN));
		dict.put(219	,new EternityPiece(NINE			,THIRTEEN	,TWENTYONE	,SIXTEEN));
		dict.put(220	,new EternityPiece(NINE			,NINETEEN	,TWENTYONE	,TWENTY));
		dict.put(221	,new EternityPiece(SIXTEEN		,SIXTEEN	,TWENTYONE	,TWENTY));
		dict.put(222	,new EternityPiece(SIXTEEN		,TWENTY		,TWENTYONE	,TWENTY));
		dict.put(223	,new EternityPiece(FOURTEEN		,TWENTY		,NINE		,NINE));
		dict.put(224	,new EternityPiece(FIFTEEN		,TWENTYTWO	,NINE		,NINE));
		//fifth row
		dict.put(225	,new EternityPiece(NINETEEN		,TWELVE		,NINE	,NINE));
		dict.put(226	,new EternityPiece(ELEVEN		,TWELVE		,NINE	,TWENTYTWO));
		dict.put(227	,new EternityPiece(FOURTEEN		,FOURTEEN	,NINE	,TWENTYTWO));
		dict.put(228	,new EternityPiece(ELEVEN		,EIGHTEEN	,NINE	,THIRTEEN));
		dict.put(229	,new EternityPiece(TWENTYTWO	,TWENTY		,NINE	,FIFTEEN));
		dict.put(230	,new EternityPiece(NINETEEN		,EIGHTEEN	,NINE	,FIFTEEN));
		dict.put(231	,new EternityPiece(FOURTEEN		,FIFTEEN	,NINE	,EIGHTEEN));
		dict.put(232	,new EternityPiece(FOURTEEN		,SIXTEEN	,NINE	,EIGHTEEN));
		//sixth row
		dict.put(233	,new EternityPiece(ELEVEN		,FIFTEEN	,ELEVEN	,ELEVEN));
		dict.put(234	,new EternityPiece(NINETEEN		,THIRTEEN	,ELEVEN	,TWENTYTWO));
		dict.put(235	,new EternityPiece(TWENTYTWO	,THIRTEEN	,ELEVEN	,THIRTEEN));
		dict.put(236	,new EternityPiece(TWENTYTWO	,FIFTEEN	,ELEVEN	,THIRTEEN));
		dict.put(237	,new EternityPiece(FOURTEEN		,TWELVE		,ELEVEN	,THIRTEEN));
		dict.put(238	,new EternityPiece(THIRTEEN		,NINETEEN	,ELEVEN	,FOURTEEN));
		dict.put(239	,new EternityPiece(TWELVE		,TWENTY		,ELEVEN	,FIFTEEN));
		dict.put(240	,new EternityPiece(SIXTEEN		,TWELVE		,ELEVEN	,FIFTEEN));
		//F
		//first row
		dict.put(241	,new EternityPiece(TWENTYTWO	,SIXTEEN	,ELEVEN		,EIGHTEEN));
		dict.put(242	,new EternityPiece(THIRTEEN		,SIXTEEN	,ELEVEN		,EIGHTEEN));
		dict.put(243	,new EternityPiece(TWELVE		,FOURTEEN	,TWENTYTWO	,TWELVE));
		dict.put(244	,new EternityPiece(FIFTEEN		,EIGHTEEN	,TWENTYTWO	,TWELVE));
		//second row
		dict.put(245	,new EternityPiece(TWENTY	,FIFTEEN	,TWENTYTWO	,THIRTEEN));
		dict.put(246	,new EternityPiece(NINETEEN	,TWENTY		,TWENTYTWO	,SIXTEEN));
		dict.put(247	,new EternityPiece(SIXTEEN	,TWENTY		,TWELVE		,TWELVE));
		dict.put(248	,new EternityPiece(FOURTEEN	,THIRTEEN	,TWELVE		,THIRTEEN));
		//third row
		dict.put(249	,new EternityPiece(THIRTEEN	,SIXTEEN	,TWELVE		,FOURTEEN));
		dict.put(250	,new EternityPiece(NINETEEN	,SIXTEEN	,TWELVE		,FIFTEEN));
		dict.put(251	,new EternityPiece(TWENTY	,EIGHTEEN	,THIRTEEN	,THIRTEEN));
		dict.put(252	,new EternityPiece(EIGHTEEN	,FOURTEEN	,THIRTEEN	,FOURTEEN));
		//fourth row
		dict.put(253	,new EternityPiece(NINETEEN	,FIFTEEN	,FOURTEEN	,EIGHTEEN));
		dict.put(254	,new EternityPiece(SIXTEEN	,NINETEEN	,FIFTEEN	,NINETEEN));
		dict.put(255	,new EternityPiece(NINETEEN	,TWENTY		,FIFTEEN	,TWENTY));
		dict.put(256	,new EternityPiece(SIXTEEN	,TWENTY		,EIGHTEEN	,TWENTY));


//		dict.remove(139);	//remove piece 139
		
		for (int i=0; i<=23; i++){
			colormap.put(i, new LinkedList<EternityPiece>());
		}
		for (int i=1; i<=256; i++){
			EternityPiece p = dict.get(i);
			colormap.get(p.north).add(p);
			colormap.get(p.east).add(p);
			colormap.get(p.west).add(p);
			colormap.get(p.south).add(p);
		}

	}

	public static void testPieces() {
		int[] temp = new int[23];
		for (int i=0; i<=22; i++){
			temp[i] = 0;
		}
		for (int i=1; i<=256; i++){
			EternityPiece p = dict.get(i);
			temp[p.north]++;
			temp[p.east]++;
			temp[p.south]++;
			temp[p.west]++;
		}
		System.out.println(Arrays.toString(temp));
		System.out.println(dict.get(2).fits(GRAY, 3, GRAY));
	}
	
	public static void testPieces2() {
		for (int i=1; i<=256; i++){
			EternityPiece p = dict.get(i);
			if (p.north==p.east && p.east==p.south){
				System.out.println(i + " - " + p);
			}
			if (p.east==p.south && p.south==p.west){
				System.out.println(i + " - " + p);
			}
			if (p.south==p.west && p.west==p.north){
				System.out.println(i + " - " + p);
			}
			if (p.west==p.north && p.north==p.east){
				System.out.println(i + " - " + p);
			}
		}
	}
	
	public static void testPieces3() {
			EternityPiece p = dict.get(139);
				System.out.println("piece 139 - " + p);
	}
	
	public static void main(String[] args) {
//		testPieces();
//		testPieces2();	
//		testPieces3();
	}
	
}
